package com.hnou.boxuegu.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.utils.MD5Utils;
import com.hnou.boxuegu.utils.UtilsHelper;

public class ModifyPswAcitivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_main_title;
    private TextView tv_back;
    private EditText et_original_psw,et_new_psw,et_new_psw_again;
    private Button btn_save;
    private String originalPsw,newPsw,newPswAgain;
    private String spOriginalPsw,userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_psw);
        init();
        userName = UtilsHelper.readLoginUserName(this);
        spOriginalPsw = UtilsHelper.readPsw(this, userName);
    }
    private void init() {
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_main_title.setText("修改密码");
        tv_back=findViewById(R.id.tv_back);
        et_original_psw=findViewById(R.id.et_original_psw);
        et_new_psw=findViewById(R.id.et_new_psw);
        et_new_psw_again=findViewById(R.id.et_new_psw_again);
        btn_save=findViewById(R.id.btn_save);
        tv_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                ModifyPswAcitivity.this.finish();
                break;
            case R.id.btn_save:
                getEditString();
                if(TextUtils.isEmpty(originalPsw)){
                    Toast.makeText(ModifyPswAcitivity.this,"请输入原始密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(!MD5Utils.md5(originalPsw).equals(spOriginalPsw)){
                    Toast.makeText(ModifyPswAcitivity.this,"输入的密码与原始密码不相同",Toast.LENGTH_SHORT).show();
                    return;
                }else if(MD5Utils.md5(newPsw).equals(spOriginalPsw)) {
                    Toast.makeText(ModifyPswAcitivity.this, "输入的密码与原始密码不能相同", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(newPsw)) {
                    Toast.makeText(ModifyPswAcitivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(newPswAgain)) {
                    Toast.makeText(ModifyPswAcitivity.this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!newPsw.equals(newPswAgain)) {
                    Toast.makeText(ModifyPswAcitivity.this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Toast.makeText(ModifyPswAcitivity.this,"新密码设置成功",Toast.LENGTH_SHORT).show();
                    //保存新密码到SharedPreferences文件中
                    UtilsHelper.saveUserInfo(ModifyPswAcitivity.this,userName,newPsw);
                    Intent intent=new Intent(ModifyPswAcitivity.this,LoginActivity.class);
                    startActivity(intent);
                    SettingActivity.instance.finish();
                    ModifyPswAcitivity.this.finish();
                }
                break;
        }
    }
    private void getEditString(){
        originalPsw=et_original_psw.getText().toString().trim();
        newPsw=et_new_psw.getText().toString().trim();
        newPswAgain=et_new_psw_again.getText().toString().trim();
    }
}